package dao

import (
	"fmt"
	"go/models"
)

func AddCourse(course models.Course) error {
	// err := db.Table("student_message").Select("sno", "sname", "ssex", "sage", "sdept", "sedu_sys", "sregion", "spolitics", "snation").Create(&student).Error
	err := db.Table("course").Create(&course).Error
	return err
}
func DelCourse(cno int) error {
	err := db.Table("course").Where("cno = ?", cno).Update("is_del", true).Error
	return err
}
func UpdateCourse(course models.Course) error {
	sno := course.Cno
	err := db.Table("course").Where("cno = ?", sno).Updates(&course).Error
	return err
}

func GetCourse(courseParam map[string]interface{}) ([]models.Course, int64, error) {
	var courseData []models.Course
	page := courseParam["page"].(int)
	pageSize := courseParam["limit"].(int)
	searchName := courseParam["searchName"].(string)
	fmt.Println(page, pageSize)
	var total int64
	err := db.Table("course").Where("cname like ? AND is_del = false", searchName+"%").Order("cno ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&courseData).Error
	fmt.Println(courseData)
	return courseData, total, err
}
